package com.michaelbulava.TFS.ui.controls.vc;

import com.intellij.openapi.project.Project;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.EventDispatcher;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.generic.IProjectDependantControl;
import com.michaelbulava.TFS.vc.tfsitem.TFSFolder;
import com.michaelbulava.TFS.vc.tfsitem.TFSItem;
import com.microsoft.tfs.core.clients.versioncontrol.path.ServerPath;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.valid.Validator;
import com.microsoft.tfs.util.valid.ValidatorHelper;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import java.awt.*;

/**
 * Created by MichaelA on 7/29/2015.
 */
public class ServerPathSelectionControl extends JPanel {//implements IProjectDependantControl {
    private JTextField serverPathTextField;
    private ServerItemTreeControl serverItemTreeControl;
    private final String pathAddition;
    private String selectedPath;
    private final ValidatorHelper validator;

    private final EventDispatcher<ChangeListener> myEventDispatcher = EventDispatcher.create(ChangeListener.class);


    public ServerPathSelectionControl(){
        this(null);
    }

    public ServerPathSelectionControl(String projectOrModuleName){
        super();
        pathAddition = projectOrModuleName;

        this.validator = new ValidatorHelper(this);

        //Initialize the panel
        setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));

        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        add(panel2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0,
                false));

        final JLabel label1 = new JLabel();
        label1.setText(Messages.getString("ServerPathSelectionControl.FolderLabelText"));
        panel2.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));

        serverPathTextField = new JTextField();
        serverPathTextField.getDocument().addDocumentListener(new DocumentAdapter() {
            @Override
            protected void textChanged(DocumentEvent event) {
                onSelectedPathModified(serverPathTextField.getText());
            }
        });
        serverPathTextField.setText(ServerPath.ROOT + pathAddition != null ? pathAddition : "");

        panel2.add(serverPathTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED,
                null, new Dimension(150, -1), null, 0, false));

        final JBScrollPane scrollPane1 = new JBScrollPane();
        add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
                false));
        serverItemTreeControl = new ServerItemTreeControl();
        serverItemTreeControl.setServerPathSelectionChangedListener(treeListener);
        scrollPane1.setViewportView(serverItemTreeControl);
    }

//    @Override
//    public void setProject(Project project) {
//        serverItemTreeControl.setProject(project);
//    }
//
//    @Override
//    public Project getProject() {
//        return serverItemTreeControl.getProject();
//    }

    public void setWorkspace(Workspace workspace){
        serverItemTreeControl.setWorkspace(workspace);
    }

    public Validator getValidator() {
        return this.validator;
    }

    protected void onSelectedPathModified(String text) {
        if (text.trim().length() == 0) {
            text = null;
        }
        this.selectedPath = text;
        if ((this.selectedPath == null) || (!ServerPath.isServerPath(this.selectedPath))) {
            this.validator.setInvalid(Messages.getString("ServerPathSelectionControl.MustSelectSingleServer"));
        } else if (ServerPath.equals("$/", this.selectedPath)) {
            this.validator.setInvalid(Messages.getString("ServerPathSelectionControl.CannotSpecifyRootPath"));
        } else if ((pathAddition != null && pathAddition.length() >0) &&
                (ServerPath.isDirectChild("$/", this.selectedPath))) {
            this.validator.setInvalid(Messages.getString("ServerPathSelectionControl.MustSpecifyPathBeneathProject"));
        } else {
            this.validator.setValid();
        }
        myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(this));
    }

    public void addChangeListener(ChangeListener value){
        myEventDispatcher.addListener(value);
    }

    private ServerItemTreeControl.ServerPathSelectionChangedListener treeListener =
            new ServerItemTreeControl.ServerPathSelectionChangedListener(){

                @Override
                public void OnTreeSelectionChanged(TFSItem selection) {
                    if (selection != null && selection instanceof TFSFolder){
                        TFSFolder folder = (TFSFolder)selection;
                        if (pathAddition != null && pathAddition.length() > 0)
                            serverPathTextField.setText(ServerPath.combine(folder.getSourceServerPath(), pathAddition));
                        else
                            serverPathTextField.setText(folder.getSourceServerPath());
                    }
                }

            };

    public String getSelectedPath(){
        return selectedPath;
    }
}
